package com.buybuyyi.stylistcrm.order.dao.impl;

import com.buybuyyi.stylistcrm.order.dao.TransferOrderGoodsDao;
import com.buybuyyi.stylistcrm.order.mapper.TransferOrderGoodsMapper;
import com.buybuyyi.stylistcrm.order.po.TransferOrderGoods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import stylistcrm.core.services.order.dto.OrderGoodsDto;
import stylistcrm.core.services.order.enums.EnumInvoiceOrderType;

import java.util.List;

/**
 * Created by cheny on 2016/9/4.
 */
@Repository
public class TransferOrderGoodsDaoImpl implements TransferOrderGoodsDao {

    @Autowired
    private TransferOrderGoodsMapper transferOrderGoodsMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return transferOrderGoodsMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(TransferOrderGoods record) {
        return transferOrderGoodsMapper.insert(record);
    }

    @Override
    public int insertSelective(TransferOrderGoods record) {
        return transferOrderGoodsMapper.insertSelective(record);
    }

    @Override
    public TransferOrderGoods selectByPrimaryKey(Integer id) {
        return transferOrderGoodsMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(TransferOrderGoods record) {
        return transferOrderGoodsMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(TransferOrderGoods record) {
        return transferOrderGoodsMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<OrderGoodsDto> getGoodsList(Long userId) {
        List<OrderGoodsDto> transferOrderGoodsList=transferOrderGoodsMapper.getGoodsList(userId);
        if(null!=transferOrderGoodsList&&!transferOrderGoodsList.isEmpty()){
            for(OrderGoodsDto orderGoodsDto:transferOrderGoodsList){
                orderGoodsDto.setOrderType(EnumInvoiceOrderType.TRANSFER_INVOICE_ORDER.getCode());
                if(orderGoodsDto.getCount()!=null&&orderGoodsDto.getDeliveredCount()!=null){
                    orderGoodsDto.setOrderDeliveredCount(orderGoodsDto.getCount()-orderGoodsDto.getDeliveredCount());
                }else{
                    orderGoodsDto.setOrderDeliveredCount(0);
                }
            }
        }
        return transferOrderGoodsList;
    }
}
